package com.jketing.entity.ams.sys;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jketing.annotation.FieldMeta;
import com.jketing.entity.SortEntity;

/**
 * Dictionary 系统字典实体
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2017-08-13
 * @since   Jpa 2.1
 */
@JsonIgnoreProperties(value= {"parent","children"})
@Entity
@Table(name = "Dictionary_")
public class Dictionary extends SortEntity<Dictionary>{
	private static final long serialVersionUID = -2414831836485608327L;
	/**
	 * parent:父级引用
	 */
	private Dictionary parent;	
	/**
	 * name:类型名称 
	 */
	@FieldMeta(name="String:name")
	private String name;
	/**
	 * enName:英文名称
	 */
	private String enName;
	/**
	 * typeCode:类别代码
	 */
	private Integer typeCode; 
	/**
	 * typeName:类别名称
	 */
	private String typeName; 	
	/**
	 * children:下级集合
	 */
	private Set<Dictionary> children = new HashSet<Dictionary>();

	/**
	 * parent 由ParentId引用的上级代码对象
	 */
	@ManyToOne
	@JoinColumn(name = "ParentId")
	public Dictionary getParent() {
		return parent;
	}
	public void setParent(Dictionary parent) {
		this.parent = parent;
	}

	/**
	 * 类型名称  
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * 英文名称（不能重复）
	 */
	public String getEnName() {
		return enName;
	}
	public void setEnName(String enName) {
		this.enName = enName;
	}
	
	/**
	 *  类别代码
	 *  1是类型；2是属性
	 */
	public Integer getTypeCode() {
		return typeCode;
	}
	public void setTypeCode(Integer typeCode) {
		this.typeCode = typeCode;
	}	
	
	/**
	 * 类别名称
	 */
	@Transient
	public String getTypeName() {
		if(typeCode!=null) {
			if(typeCode == 1) {
				typeName = "类型"; 
			}
			else if(typeCode == 2){
				typeName = "属性";
			}else {
				typeName = "未定类型";
			}
			return typeName;
		}else {
			return "类型空";
		}
		
	}
	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}

	/**
	 * 直接下级
	 */
	@OneToMany(mappedBy = "parent",
								cascade = CascadeType.ALL, 
								fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Dictionary> getChildren() {
		return children;
	}
	public void setChildren(Set<Dictionary> children) {
		this.children = children;
	}
	
	/**
	 * 增加下级
	 * @param entity
	 */
	public void add(Dictionary entity) {
		if (!children.contains(entity)) {
			children.add(entity);
			entity.setParent(this);
		}
	}
	
	/**
	 * 删除下级
	 * @param Dictionary
	 */
	public void remove(Dictionary entity) {
		if (children.contains(entity)) {
			entity.setParent(null);
			children.remove(entity);
		}
	}

	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Dictionary)) {
			return false;
		}
		Dictionary other = (Dictionary) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
	
	/**
	 * compareTo Comparable接口方法
	 * 	实体对象比较
	 * 	根据排序号差比较：本实例排序号减去其它同类实例排序号
	 * 	差大于0返回1
	 * 	差等于0返回0
	 * 	差小于0返回-1
	 * 	参数other为null或者不是本类的实例返回-1
	 * 
	 * @param  other 其它同类实例
	 */
	public int compareTo(Dictionary other) {
		if (other == null || !(other instanceof Dictionary))
			return -1;
		long temp = orderNumber - other.getOrderNumber();
		if (temp > 0)
			return 1;
		else if (temp == 0)
			return 0;
		else
			return -1;
	}
}
